<?php
/**
  *  www.Upin.com域名使用接口
  *
  */
header("Content-Type: text/html; charset=utf-8");
require_once '../baseClasses/config_ser.php';
//require_once '../baseClasses/config_loc.php';
require_once '../baseClasses/dbConn.php';

$db = new dbConn();


$act = isset($_GET["act"]) ? htmlspecialchars($_GET["act"]) : "";

switch( $act ){
	case 'del':
		
		break;
	case 'insert':
		//限定域名
		if( isset( $_REQUEST['message'] )  ){//&& !(strpos($_SERVER['HTTP_HOST'], 'unpin.com') === FALSE)
			$callback = isset($_GET['callback'])?trim($_GET['callback']):'';
			$time = time();
			$db->sql = "insert into upin_message (m_time,m_message) values ({$time},'{$_REQUEST['message']}')";
			$db->doExecute();
			$json = json_encode(array('msg'=>'成功！','state'=>1));
			echo "{$callback}({$json})";
		}else{
			$json = json_encode(array('msg'=>'请求错误！','state'=>1));
			echo "{$callback}({$json})";
		}
		break;
	case 'upload':
		
		break;
	case 'paramts':
		$type = isset($_GET["type"]) ? htmlspecialchars($_GET["type"]) : "";
			switch( $type ){
				//所有小编
				case '1':
					$db->sql = "SELECT * FROM `admin`";
					$db->doResultList();
					$paramts = $db->result;
					break;
				//最新小编说
				case '2':
					$db->sql = "SELECT * FROM `bottomwords` order by `pdate` desc limit 1";
					$db->doResult();
					$paramts = $db->result;
					break;
			}
			echo json_encode( $paramts );
		break;
	case 'show':
	default:
		$type = isset($_GET["type"]) ? htmlspecialchars($_GET["type"]) : "";
		$date = isset($_GET["date"]) ? htmlspecialchars($_GET["date"]) : "";
		$callback = isset($_GET["callback"]) ? htmlspecialchars($_GET["callback"]) : "";
		
		
		
		if($type)
		{
			//$type == 1 : 今日推荐
			if($type==1 && $date)
			{
				$db->sql = "SELECT c1.shared,c1.collected,c1.id,c1.updater,c1.publishDate,c1.picture,c1.title,CONCAT(c1.goodsUrl,'&outer_code=',a.id,'_') goodsUrl,c1.`desc`,c1.originalPrice,c1.currentPrice,c1.sort,c1.cateID,c2.cate cateName,c1.saledNum,c1.goodsID,c1.commission FROM `content` c1 LEFT JOIN `category` c2 ON c1.cateID=c2.id LEFT JOIN `admin` a ON c1.updater = a.`userName` WHERE c1.status=1 AND c1.isDel=0 AND c1.publishDate = '".trim($date)."'  ORDER BY c1.sort ASC,c1.id ASC ";
				$db->doResultList2();
			}
			
			//$type == 2 : 9.9包邮
			elseif($type==2 && $date)
			{
				$db->sql = "SELECT p.id,p.publishDate,p.picture,p.title,CONCAT(p.goodsUrl,'&outer_code=',a.id) goodsUrl,p.`desc`,p.originalPrice,p.currentPrice,p.sort,p.saledNum,p.startTime,p.endTime,p.goodsID,p.commission FROM `priceninepointnine` p LEFT JOIN `admin` a ON p.updater = a.`userName` WHERE p.`status`=1 AND p.isDel=0 AND p.publishDate = '".$date."'";
				$db->doResultList2();
			}
			
			//$type == 3 : 19.9包邮
			elseif($type==3)
			{
				$db->sql = "SELECT p.id,p.publishDate,p.picture,p.title,CONCAT(p.goodsUrl,'&outer_code=',a.id) goodsUrl,p.`desc`,p.originalPrice,p.currentPrice,p.sort,p.saledNum,p.startTime,p.endTime,p.goodsID,p.commission FROM `priceninetenpointnine` p LEFT JOIN `admin` a ON p.updater = a.`userName` WHERE p.`status`=1 AND p.isDel=0 AND p.publishDate = '".$date."'";
				$db->doResultList2();
			}
			else 
			{
				echo json_encode(array("msg"=>"error"));
				exit;
			}
			
			$data = $db->result;
			foreach ($data as $key => $val){
				if( ! preg_match("/^http\:\/\//", $val['picture']))
				{
					$data[$key]['picture'] = "http://app100656353.qzone.qzoneapp.com/".$val['picture'];
				}
			
				if($type == 2 || $type == 3){
					$now = time();
					$data[$key]['CountDown'] = 0;
					if($now > $val['startTime'] && $now < $val['endTime']){
						$data[$key]['CountDown'] = $val['endTime'] - $now;
					}
				}
			}
			
			if( $type==1 && $data ){
				$db->sql = "SELECT * FROM `comment` where turndate='{$date}' order by `order` asc";
				$db->doResultList();
				$pinglun = $db->result;
				$db->sql = "SELECT * FROM `virtualuser` where turndate='{$date}' order by `id` asc";
				$db->doResultList();
				$virtualuser = $db->result;
				
				foreach ($data as $key => $val){
					for( $i=$key*3; $i<($key+1)*3; $i++ ){
						$pinglun[$i]['face'] = $virtualuser[$i]['pic'];
						$data[$key]['pinglun'][] = $pinglun[$i];
					}
					switch(($key%15)+1){
						case 1: $_collected += 2659; $_shared += 1578; break;
						case 2: $_collected += 1865; $_shared += 1268; break;
						case 3: $_collected += 2187; $_shared += 1654; break;
						case 4: $_collected += 1356; $_shared += 987; break;
						case 5: $_collected += 2014; $_shared += 1475; break;
						case 6: $_collected += 1532; $_shared += 1021; break;
						case 7: $_collected += 3045; $_shared += 1128; break;
						case 8: $_collected += 1658; $_shared += 921; break;
						case 9: $_collected += 1058; $_shared += 754; break;
						case 10: $_collected += 2048; $_shared += 565; break;
						case 11: $_collected += 1077; $_shared += 808; break;
						case 12: $_collected += 1206; $_shared += 609; break;
						case 13: $_collected += 589; $_shared += 510; break;
						case 14: $_collected += 691; $_shared += 207; break;
						case 15: $_collected += 1503; $_shared += 1309; break;
					}
					$data[$key]['shared'] = $_shared;
					$data[$key]['collected'] = $_collected;
					$db->sql = "SELECT * FROM `comment2` where contentid={$val['id']} order by date desc limit 0,4";
					$db->doResultList();
					$data[$key]['pinglun2'] = $db->result;
				}
			}
			
			$result = json_encode($data);	
			echo $callback ? $callback."($result)" : $result;
		}
		else
		{
			echo json_encode(array("msg"=>"error"));
			exit;
		}
		break;
}